package com.example.qihuibao.utils.extension

import com.example.qihuibao.utils.MMKVUtil
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 *     author : cwwei
 *     e-mail : 494545699@qq.com
 *     date   : 2022/08/18
 *     desc   : 委托属性，被委托的变量在赋值和读取时，会分别自动调用下面的setValue和getValue方法
 */
class AccessDelegate<T>(private val key:String, private val default: T) : ReadWriteProperty<Any?, T> {
    override fun getValue(thisRef: Any?, property: KProperty<*>) : T {
        return MMKVUtil.getValue(key, default!!) as T
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        MMKVUtil.setValue(key, value!!)
    }
}